/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.anis.project.medcare.views;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

import com.anis.project.medcare.Activator;
import com.anis.project.medcare.dicom.viewer.view.DicomImageHandler;
import com.anis.project.medcare.dicom.viewer.view.SWTImageCanvas;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;

public class DicomViewer extends ViewPart {
	SWTImageCanvas imageCanvas;
	Slider slider;
	Action saveAction;
	private List<File> sourceFile;

	/**
	 * The constructor.
	 */

	public static String ID = "com.anis.project.medcare.views.dicom.viewer";

	DicomImageHandler dicomImageHandler;
	Composite parent;
	SWTImageCanvas swtImageCanvas;
	ArrayList<ImageData> dicomImagesList;
	List<java.awt.Image> images;
	int currentImageIndex;
	private Group group0;
	private Button zoomOutButton;
	private Button zoomInButton;

	public DicomViewer() {
		images = new ArrayList<java.awt.Image>();
		currentImageIndex = 0;
	}

	/**
	 * Create the GUI.
	 * 
	 * @param frame
	 *            The Composite handle of parent
	 */
	public void createPartControl(Composite composite) {
		composite.setLayout(new GridLayout(1, false));
		swtImageCanvas = new SWTImageCanvas(composite, SWT.FILL);
		swtImageCanvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1));

		Group group = new Group(composite, SWT.NONE);
		group.setLayout(new GridLayout(1, false));
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));

		slider = new Slider(group, SWT.NONE);

		slider.setMinimum(0);
		slider.setIncrement(1);
		slider.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				swtImageCanvas.loadImage((ImageData) dicomImagesList.get(slider
						.getSelection()));
				slider.setToolTipText("Slide Number :" + slider.getSelection());
			}
		});
		slider.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		group0 = new Group(composite, SWT.BORDER);
		group0.setLayout(new FillLayout(SWT.HORIZONTAL));
		GridData gd_group0 = new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1);
		gd_group0.heightHint = 22;
		group0.setLayoutData(gd_group0);

		zoomOutButton = new Button(group0, SWT.NONE);
		zoomOutButton.setText("-");

		zoomOutButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				swtImageCanvas.zoomOut();

			}
		});

		zoomInButton = new Button(group0, SWT.NONE);
		zoomInButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				swtImageCanvas.zoomIn();

			}
		});
		zoomInButton.setText("+");
	}

	/**
	 * Called when we must grab focus.
	 * 
	 * @see org.eclipse.ui.part.ViewPart#setFocus
	 */
	public void setFocus() {

	}

	/**
	 * Called when the View is to be disposed
	 */
	public void dispose() {
		super.dispose();
		Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
				.getActivePage().setEditorAreaVisible(true);
	}

	public void setFileSource(List<File> dicomFile) {
		this.sourceFile = dicomFile;
		if (sourceFile != null) {
			DicomImageHandler dicomImageHandler = new DicomImageHandler(
					sourceFile);
			dicomImagesList = dicomImageHandler.createDicomImage();
			slider.setMaximum(dicomImagesList.size());
			swtImageCanvas.loadImage((ImageData) dicomImagesList.get(0));
		}

	}

	@Override
	public void init(IViewSite site) throws PartInitException {
		// TODO Auto-generated method stub
		super.init(site);
	}

	public void setDirectorySource(String pathToDicomFile) {
		File folder = new File(pathToDicomFile);
		List<File> dicomFile = new ArrayList<File>();
		File[] listOfFiles = folder.listFiles();
		for (File file : listOfFiles) {
			if (file.isFile() && file.getName().contains(".dcm")) {
				System.out.println(file.getName());
				dicomFile.add(file);
			}
		}
		setFileSource(dicomFile);
	}

	public ArrayList<ImageData> getAllDicomData() {
		return dicomImagesList;
	}

}